package org.dromara.nursing.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.nursing.domain.bo.MsContactInfoBo;
import org.dromara.nursing.domain.vo.MsContactInfoVo;
import org.dromara.nursing.domain.MsContactInfo;
import org.dromara.nursing.mapper.MsContactInfoMapper;
import org.dromara.nursing.service.IMsContactInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 联系人信息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-03-16
 */
@RequiredArgsConstructor
@Service
public class MsContactInfoServiceImpl implements IMsContactInfoService {

    private final MsContactInfoMapper baseMapper;

    /**
     * 查询联系人信息
     *
     * @param contactId 主键
     * @return 联系人信息
     */
    @Override
    public MsContactInfoVo queryById(Long contactId){
        return baseMapper.selectVoById(contactId);
    }

    /**
     * 分页查询联系人信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 联系人信息分页列表
     */
    @Override
    public TableDataInfo<MsContactInfoVo> queryPageList(MsContactInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MsContactInfo> lqw = buildQueryWrapper(bo);
        Page<MsContactInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的联系人信息列表
     *
     * @param bo 查询条件
     * @return 联系人信息列表
     */
    @Override
    public List<MsContactInfoVo> queryList(MsContactInfoBo bo) {
        LambdaQueryWrapper<MsContactInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MsContactInfo> buildQueryWrapper(MsContactInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MsContactInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(MsContactInfo::getContactId);
        lqw.eq(bo.getElderId() != null, MsContactInfo::getElderId, bo.getElderId());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), MsContactInfo::getContactName, bo.getContactName());
        lqw.eq(bo.getGender() != null, MsContactInfo::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), MsContactInfo::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), MsContactInfo::getIdCard, bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationship()), MsContactInfo::getRelationship, bo.getRelationship());
        lqw.eq(bo.getIsEmergencyContact() != null, MsContactInfo::getIsEmergencyContact, bo.getIsEmergencyContact());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), MsContactInfo::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), MsContactInfo::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getVisitFrequency()), MsContactInfo::getVisitFrequency, bo.getVisitFrequency());
        lqw.eq(bo.getReceiveNotification() != null, MsContactInfo::getReceiveNotification, bo.getReceiveNotification());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), MsContactInfo::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增联系人信息
     *
     * @param bo 联系人信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MsContactInfoBo bo) {
        MsContactInfo add = MapstructUtils.convert(bo, MsContactInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setContactId(add.getContactId());
        }
        return flag;
    }

    /**
     * 修改联系人信息
     *
     * @param bo 联系人信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MsContactInfoBo bo) {
        MsContactInfo update = MapstructUtils.convert(bo, MsContactInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MsContactInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除联系人信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
